<?php
/**
 * Created by PhpStorm.
 * User: 虚空之翼 <183700295@qq.com>
 * Date: 16/3/25
 * Time: 09:12
 * 微信事件接口
 */
namespace app\admin\controller;

use app\admin\model\WechatLog;
use app\admin\model\WechatUser;
use com\wechat\QYWechat;
use com\wechat\TPQYWechat;
use com\wechat\TPWechat;
use Endroid\QrCode\QrCode;
use think\Config;
use think\Controller;
use think\Log;

class Service extends Controller
{

    // 服务号接收的应用
    public function event() {
        $Wechat = new TPQYWechat(Config::get('party'));
        $Wechat->valid();

        $type = $Wechat->getRev()->getRevType();
        switch ($type) {
            case QYWechat::MSGTYPE_TEXT:
                $Wechat->text("您好！感谢关注！")->reply();
                break;
            case QYWechat::MSGTYPE_EVENT:
                $event = $Wechat->getRev()->getRevEvent();
                switch ($event['event']) {
                    case 'subscribe':
                        $replyText = "您好！欢迎关注 两美箬横 党建！";
                        $Wechat->text($replyText)->reply();
//                        $newsData = array(
//                            '0'=> array(
//                                'Title' => "欢迎您关注“香市机关党建”",
//                                'Description' => "内含企业二维码，可转发给同事关注",
//                                'PicUrl' => "http://xspb.0571ztnet.com/home/images/special/music_2.jpg",
//                                'Url' => "http://u3665579.viewer.maka.im/pcviewer/WRZ0GJFA",
//                            ),
//                        );
//                        $Wechat->news($newsData)->reply();
                        break;
                    case 'enter_agent':
                        $data = array(
                            'event' => $event['event'],
                            'msgtype' => $type,
                            'agentid' => $Wechat->getRev()->getRevAgentID(),
                            'create_time' => $Wechat->getRev()->getRevCtime(),
                            'event_key' => isset($event['key']) ? $event['key'] : '',
                            'userid' => $Wechat->getRev()->getRevFrom()
                        );
//                        Log::record("进入事件：".json_encode($data));
                        $id  = WechatLog::create($data);
//                        Log::record("创建记录：".$id);
                        //$Wechat->text(json_encode($data))->reply();
                        save_log($Wechat->getRev()->getRevFrom(), 'WechatLog');
                        break;
                }
                break;
            case QYWechat::MSGTYPE_IMAGE:
                break;
            default:
                $Wechat->text("您好！感谢关注！")->reply();
        }

    }

    // 企业号验证
    public function oauth() {
        $weObj = new TPQYWechat(Config::get('party'));
        $weObj->valid();
    }

    //订阅号验证
    public function oauth2(){
        $Wechat = new TPWechat(Config::get('news'));
        $Wechat->valid();
    }

    public function media() {
        $Wechat = new TPWechat(Config::get('news'));
        $list = $Wechat->getForeverList("news", 0, 20);
        dump($list);
    }

    /**
     *生成二维码
     */
    public function qrcode()
    {
        $data = WechatUser::where(['id' => ['egt',3884], 'coach_id' => 18758887835])->select();
//        var_dump($data);die;
//        $data = WechatUser::where(['id' => ['egt',3873]])->where(['id' => ['elt',3880]])->select();
        $n = 0;
        foreach ($data as $v) {
            //二维码内容
            //$request = Request::instance();
            $msg = $v['userid'];
            $str = preg_replace("/([\x{4e00}-\x{9fa5}])/u", "\\1 ", $v['name']);
            $res = preg_replace ( "/\s(?=\s)/","\\1", $str);
            $qrCode = new QrCode($msg);
            $qrCode->setSize(300);
            $qrCode->setMargin(10);
            $qrCode->setEncoding('UTF-8');
            $qrCode->setLabel($res,36);
            $path = './uploads/picture/' . date('Ymd');

            if (!is_dir($path)) {
                mkdir($path, 0777, true);
            }

            $name = $v['name'] .' '.$v['mobile']. '.png';
            $qrCode->writeFile($path . '/' . $name);
            ltrim($path.'/'.$name, ".");
            $n++;
        }

        return '已生成 '.$n.' 个二维码!';
    }
}